package com.ruoyi.web.controller.system.carPack;

import java.util.List;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.carPark.FzCarInOut;
import com.ruoyi.system.domain.communiry.FzSCommunity;
import com.ruoyi.system.service.carPark.IFzCarInOutService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 进场记录Controller
 * 
 * @author ruoyi
 * @date 2020-03-03
 */
@Controller
@RequestMapping("/system/out")
public class FzCarInOutController extends BaseController
{
    private String prefix = "system/carPark/out";

    @Autowired
    private IFzCarInOutService fzCarInOutService;

    @RequiresPermissions("system:out:view")
    @GetMapping()
    public String out(HttpSession session)
    {
        return prefix + "/out";
    }

    /**
     * 查询进场记录列表
     */
    @RequiresPermissions("system:out:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpSession session,FzCarInOut fzCarInOut)
    {
        startPage();
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        fzCarInOut.setcId(communityId);
        List<FzCarInOut> list = fzCarInOutService.selectFzCarInOutList(fzCarInOut);
        return getDataTable(list);
    }

    /**
     * 导出进场记录列表
     */
    @RequiresPermissions("system:out:export")
    @Log(title = "进场记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzCarInOut fzCarInOut)
    {
        List<FzCarInOut> list = fzCarInOutService.selectFzCarInOutList(fzCarInOut);
        ExcelUtil<FzCarInOut> util = new ExcelUtil<FzCarInOut>(FzCarInOut.class);
        return util.exportExcel(list, "out");
    }

    /**
     * 新增进场记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存进场记录
     */
    @RequiresPermissions("system:out:add")
    @Log(title = "进场记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzCarInOut fzCarInOut)
    {
        fzCarInOut.setCarInOutId(HeadNum.CAR_IN_OUT_HEAD+ DateUtils.getTimeMillis());
        fzCarInOut.setcId(GetSessionValueUtil.getCommunity());
        SysUser user = ShiroUtils.getSysUser();
        fzCarInOut.setCreateName(user.getUserName());
        return toAjax(fzCarInOutService.insertFzCarInOut(fzCarInOut));
    }

    /**
     * 修改进场记录
     */
    @GetMapping("/edit/{carInOutId}")
    public String edit(@PathVariable("carInOutId") String carInOutId, ModelMap mmap)
    {
        FzCarInOut fzCarInOut = fzCarInOutService.selectFzCarInOutById(carInOutId);
        mmap.put("fzCarInOut", fzCarInOut);
        return prefix + "/edit";
    }

    /**
     * 修改保存进场记录
     */
    @RequiresPermissions("system:out:edit")
    @Log(title = "进场记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzCarInOut fzCarInOut)
    { SysUser user = ShiroUtils.getSysUser();
    fzCarInOut.setUpdateName(user.getUserName());
        return toAjax(fzCarInOutService.updateFzCarInOut(fzCarInOut));
    }

    /**
     * 删除进场记录
     */
    @RequiresPermissions("system:out:remove")
    @Log(title = "进场记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzCarInOutService.deleteFzCarInOutByIds(ids));
    }
}
